/**
 * <strong>Title : PartController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月6日 上午11:13:16<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.Part;
import cn.b.sky.mes.em.model.condition.PartCondition;
import cn.b.sky.mes.em.service.PartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : PartController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备控制器<br> 
 * <strong>Create on : 2015年11月6日 上午11:13:16<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/equipmentPart/*")
public class PartController extends SecurityController {

  @Resource
  PartService partService;

  @RequestMapping(value = "part_list")
  public String selectPartListByCondition(String navTabId, Model model,
      @ModelAttribute("condition") PartCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<Part> list = partService.selectPartListByCondition(condition);
    int count = partService.selectPartCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "em/part/part_list";
  }

  @RequestMapping(value = "addPart")
  public String addPart(String navTabId, Model model) {

    model.addAttribute("pattern", "add");
    model.addAttribute(NAVTABID, navTabId);
    return "em/part/part_add";
  }

  @RequestMapping(value = "modifyPart")
  public String modifyPart(String navTabId, Model model, String id) {
    Part part = partService.getPart(id, getSiteCode());
    model.addAttribute("pattern", "modify");
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("part", part);
    return "em/part/part_add";
  }

  @RequestMapping(value = "viewPart")
  public String viewPart(String navTabId, Model model, String id) {
    Part part = partService.getPart(id, getSiteCode());
    model.addAttribute("pattern", "read");
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("part", part);
    return "em/part/part_add";
  }

  @RequestMapping("validatePartCode")
  @ResponseBody
  public boolean validatePartCode(String partCode) {
    if (!StringUtils.isEmpty(partCode)) {
      return partService.getPart(partCode, getSiteCode()) == null;
    } else {
      return true;
    }
  }

  @RequestMapping(value = "savePart")
  @ResponseBody
  public DWZResult savePart(String navTabId, Model model, String id, Part part) {
    DWZResult error = ajaxError(getImeViewMessageI18n(null,
        SystemConstant.PARAMETERNULL));
    if (part == null || StringUtils.isBlank(part.getPartCode())) {
      return error;
    }
    part.setSiteCode(getSiteCode());
    setCreateInfo(part);
    if (part.getLockFlag() == null) {
      part.setLockFlag(0);
    }

    if (StringUtils.isEmpty(id)) {
      partService.insertPart(part);
    } else {
      part.setPartCode(id);
      partService.updatePart(part);
    }

    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);
  }

  @RequestMapping("deletePart")
  @ResponseBody
  public DWZResult deletePart(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = partService.deletePart(codes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

  @RequestMapping("checkPartCode")
  @ResponseBody
  public boolean checkPartCode(String partCode) {
    if (!StringUtils.isEmpty(partCode)) {
      Part part = partService.getPart(partCode, getSiteCode());
      return part != null && part.getLockFlag() != 1;
    } else {
      return false;
    }
  }

  @RequestMapping("partSuggest")
  @ResponseBody
  public Object partSuggest(String navTabId, Model model, @ModelAttribute("condition") PartCondition condition,
      PartCondition condition1) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<Part> list = partService.selectPartListForSuggest(condition);
    condition.setData(list);
    return list;
  }

  @RequestMapping(value = "partLookup")
  public String partLookup(Model model, @ModelAttribute("condition") PartCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<Part> list = partService.selectPartListByCondition(condition);
    int count = partService.selectPartCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "em/part/part_lookup";
  }

  @RequestMapping("partLookUpForOne")
  public String partLookUpForOne(Model model, @ModelAttribute("condition") PartCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<Part> list = partService.selectPartListByCondition(condition);
    int count = partService.selectPartCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "em/part/part_select";

  }

  @RequestMapping(value = "validatePartCodeForSelect")
  @ResponseBody
  public boolean validatePartCodeForSelect(String partCode) {
    if (!StringUtils.isEmpty(partCode)) {
      Part part = partService.getPart(partCode, getSiteCode());
      if (part != null) {
        return part.getLockFlag() == 0;
      }
    }
    return false;
  }
}
